//
//  HeaderView.m
//  cookBook
//
//  Created by xalo on 16/1/26.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#import "HeaderView.h"
#import "Model.h"
#import <UIImageView+WebCache.h>
#define kLeft 10
#define kVi 10
#define kTextH 30
@implementation HeaderView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //图片
        CGFloat thumbX = 0;
        CGFloat thumbY = 0;
        CGFloat thumbW = kWidth;
        CGFloat thumbH = 200;
        
        self.thumbImageView = [[UIImageView alloc] initWithFrame:CGRectMake(thumbX, thumbY, thumbW, thumbH)];
//        self.thumbImageView.backgroundColor = [UIColor redColor];
        [self addSubview:self.thumbImageView];
        
        //标题
        CGFloat titleX = kLeft;
        CGFloat titleY = thumbH -40;
        CGFloat titleW = thumbW - 2*kLeft;
        CGFloat titleH = kTextH;
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleX, titleY, titleW, titleH)];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
//        self.titleLabel.backgroundColor = [UIColor redColor];
        [self addSubview:self.titleLabel];
        
        //简介
        CGFloat jjieX = kLeft;
        CGFloat jjieY = thumbY + thumbH + kVi;
        CGFloat jjieW = titleW;
        CGFloat jjieH = kTextH;
        
        self.jianjieLabel = [[UILabel alloc] initWithFrame:CGRectMake(jjieX, jjieY, jjieW, jjieH)];
        self.jianjieLabel.font = [UIFont systemFontOfSize:14];
        self.jianjieLabel.numberOfLines = 0;
//        self.jianjieLabel.backgroundColor = [UIColor redColor];
        [self addSubview:self.jianjieLabel];
        
        
        
    }

    return self;

}


-(void)setModel:(Model *)model
{
    
    _model = model;
    
    //图片
    if (model.pictureImage == nil) {
        
        [self.thumbImageView sd_setImageWithURL:[NSURL URLWithString:model.thumb] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            model.pictureImage = image;
        }];
        
    }
    
    self.thumbImageView.image = model.pictureImage;
    //标题
    self.titleLabel.text = model.title;
    
    //简介
    CGRect frame = self.jianjieLabel.frame;
    frame.size.height = [HeaderView heightForString:model.jianjie];
    
    self.jianjieLabel.frame = frame;
    
    self.jianjieLabel.text = model.jianjie;
    
    
    
}

//自适应高度方法实现

+(CGFloat)heightForString:(NSString *)string
{
    //1.获取字体属性
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
    //2.调用string的绘制方法.绘制矩形
    
    CGFloat labelWidth = kWidth - 2*kLeft;
    CGRect bounds = [string boundingRectWithSize:(CGSizeMake(labelWidth, 2000)) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    
    //把绘制后的矩形高度返回
    return bounds.size.height;
    
}

//单元格高度
+(CGFloat)HeaderViewHeightForModel:(Model *)model
{
    
    //自适应高度  =文本高度 + 固有高度
    CGFloat adjustHeight = [HeaderView heightForString:model.jianjie] + 200 + 2*kLeft;
    
    return adjustHeight;
    
}






@end
